﻿/// <reference path="../Script/ext-4.1.1a/test.js" />
Ext.require([
    'Ext.tree.*',
    'Ext.data.*',
    'Ext.window.MessageBox',
    'Ext.form.*',
    'Ext.window.Window',
]);
Ext.ns("alex.extjs.layout");
alex.extjs.layout.PrettyPanel = function () {
    Ext.BLANK_IMAGE_URL = 'Script/ext-4.1.1a/resources/themes/images/default/tree/s.gif';
    Ext.QuickTips.init(); //初始化快速提示 
    var westPanel = new Ext.panel.Panel({
        id: 'tree-panel',
        title: '管理菜单',
        region: 'west',
        layout: 'accordion',
        split: true,
        collapsible: true,
        margins: '3 0 8 5', //当前组件的西边页边距为5 
        width: 160,
        rootVisible: false,
        bodyStyle: 'background:#eee;',
        autoScroll: true,
        checkModel: false,
        useArrows: true,
        frame: true
    });
    // 组建树  
    var buildTree = function (json) {
        return Ext.create('Ext.tree.Panel', {
            title: json.text,
            rootVisible: false,
            border: false,
            store: Ext.create('Ext.data.TreeStore', {
                root: {
                    expanded: true,
                    children: json.children
                }
            }),
            listeners: {
                'itemclick': treeClick,
                scope: this
            }
        });
    };
    /** 
    * 加载菜单树   */
    Ext.Ajax.request({
        url: 'Module/BaseSys/MenuTree.ashx',
        method: 'post',
        success: function (response) {
            var json = Ext.JSON.decode(response.responseText);
            Ext.each(json, function (el) {
                westPanel.add(buildTree(el));
            });
        },
        failure: function (request) {
            Ext.MessageBox.show({
                title: '操作提示',
                msg: "连接服务器失败",
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.ERROR
            });
        }
    });
    var southPanel = new Ext.Panel({        
        region: 'south',
        title: '', //面板标题 
        height: 2, //指定高度 
        collapsible: false, //可以折叠 
        frame: true
    });
    var centerPanel = Ext.createWidget('tabpanel', {
        region: 'center',    //边界布局,必须有 center 
        id: 'center',
        margins: '3 4 8 0',
        minTabWidth: 80,
        enableTabScroll: true,
        deferredRender: false,
        scripts: true,
        activeTab: 0,
        plugins: Ext.create('Ext.ux.TabCloseMenu', {
            closeTabText: '关闭',
            closeOthersTabsText: '关闭其他',
            closeAllTabsText: '关闭所有'           
        }),
        items: [{
            title: '我的桌面', //'所有文档',
            html: '<div style="font-size:16px; float: left; padding:20px 0 0 50px;"><a href="DownLoad/中电方大文档维护客户端.rar">下载管理客户端</a><span style="font-size: 12px;">&nbsp;&nbsp;(注:仅供上传文档的管理员使用)</span><br /><br />系统使用帮助文档:&nbsp;&nbsp;<a href="#" onclick="openHelp()">html版</a>&nbsp;<a href="DownLoad/中电方大内部文档管理系统使用说明.docx">word版</a>'
        }]
    });
    var northPanel = new Ext.Panel({
        region: 'north',
        title: '', //面板标题 
        height: 60, //指定高度,
        collapsible: false, //可以折叠 
        html: '<div style="width:100%; background-color:#46B7FF;"><embed height="60" width="600" flashvars="bannerWidth=600&bannerHeight=60&bannerSID=http://img2.tbcdn.cn/tfscom/T1FWhpXqXaXXXgzXjX.xml&bannerXML=&bannerLink=http%3A%2F%2F&dataSource=&bid=34797678&appSource=default" wmode="transparent" allowscriptaccess="always" quality="high" name="34797678" id="34797678" style="" src="http://img.uu1001.cn/bcv3.swf?v=20121219" type="application/x-shockwave-flash"/></embed></div>',
        frame: true
    });
    var viewport = new Ext.Viewport({//生成一个 ExtJS 视窗 组件对象 
        renderTo: Ext.getBody(),    //呈现在 Html Body标签中 
        layout: 'border',    //使用边界布局 
        items: [
		    northPanel,
		    centerPanel,
		    southPanel,
		    westPanel
        ]
    });
    // 设置树的点击事件
    function treeClick(view, record, item, index, e) {
        if (record.raw.leaf) {
            e.stopEvent();
            var n = centerPanel.getComponent(record.id);
            if (!n) {
                var targetUrl = "down.html";
                if (record.raw.url != undefined && record.raw.url != "") {
                    targetUrl = record.raw.url;
                }
                var n = centerPanel.add({
                    id: record.id,
                    title: record.raw.text,
                    layout: 'fit',
                    closable: true, 
                    //tooltip: record.raw.text,                      
//                     initEvents: function () {  //双击删除
//                        var obj = this;
//                        var id = 'tab-' + (this.id.split('-')[1] - 1 + 2) + '-btnWrap';
//                        Ext.get(id).dom.ondblclick = function () {
//                            if (obj.title != '我的桌面')
//                                centerPanel.remove(obj);
//                        }
//                    },                                      
                    autoLoad: {
                        url: targetUrl, //如若使用iframe，则每页需要重新加载Ext
                        scripts: true
                    }// 通过autoLoad属性载入目标页,如果要用到脚本,必须加上scripts属性
                });
            }
            centerPanel.setActiveTab(n);
        }
    }



    var themes = [
            ['默认', 'ext-all.css'],
            ['紫色', 'xtheme-purple.css'],
            ['橄榄色', 'xtheme-olive.css'],
            ['暗蓝色', 'xtheme-slate.css'],
            ['浅灰色', 'xtheme-darkgray.css'],
            ['绿色', 'xtheme-gray.css'],
            ['椒盐色', 'xtheme-galdaka.css'],
            ['粉色', 'xtheme-pink.css'],
            ['靛青色', 'xtheme-indigo.css'],
            ['深夜', 'xtheme-midnight.css'],
            ['银白色', 'xtheme-silverCherry.css']
          ];
    var Mystore = new Ext.data.SimpleStore({
        fields: ['theme', 'css'],
        data: themes
    });

    // 定义下拉列表框：
    var Mycombo = new Ext.form.ComboBox({
        fieldLabel: '更换皮肤',
        id: 'css',
        triggerAction: 'all',
        store: Mystore,
        displayField: 'theme',
        valueField: 'css',
        value: '默认',
        mode: 'local',
        anchor: '95%',
        handleHeight: 10,
        resizable: true,
        selectOnfocus: true,
        typeAhead: true
    });
    Mycombo.on //定义事件
            ({
                "select": function () {
                    var css = Mycombo.getValue();
                    //设置cookies
                    var date = new Date();
                    //alert(css);
                    date.setTime(date.getTime() + 30 * 24 * 3066 * 1000);
                    document.getElementsByTagName("link")[1].href = "ext-3.2.0/resources/css/" + css;
                    document.cookie = "css=" + css + ";expires=" + date.toGMTString();
                }
            });
          

};
Ext.onReady(alex.extjs.layout.PrettyPanel);

